import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../components/common/Login";
import Main from "../components/admin/Main";
import Welcome from "../components/common/Welcome";
import Druid from "../components/admin/Druid";
import Users from "../components/admin/Users";
import Home from "../components/userview/Home";
import AddGroup from "../components/admin/AddGroup";
import UserList from "../components/userview/UserList";
import GiveUserToGroup from "../components/userview/GiveUserToGroup";
import MyGroup from "../components/userview/MyGroup";
import CreatePlan from "../components/userview/CreatePlan";
import UploadFile from "../components/userview/UploadFile";
import DailyFile from "../components/userview/DailyFile";
import ProjectFile from "../components/userview/ProjectFile";
import Book from "../components/userview/Book";
import AdminPlan from "../components/admin/AdminPlan";
import Report from "../components/admin/Report";
import ProjectInformation from "../components/admin/ProjectInformation";

Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    {path: '/',redirect: '/login'},
    {path: '/login',component: Login},
    {
      path: '/main',
      component: Main,
      redirect: '/welcome',
      children: [
        {path: '/welcome',component: Welcome},
        {path: '/druid',component: Druid},
        {path: '/users',component: Users},
        {path: '/addGroup',component: AddGroup},
        {path: '/adminPlan',component: AdminPlan},
        {path: '/adminUploadFile',component: UploadFile},
        {path: '/getAdminDailyFile',component: DailyFile},
        {path: '/getAdminProjectFile',component: ProjectFile},
        {path: '/getAdminBook',component: Book},
        {path: '/getReport',component: Report},
        {path: '/getProjectInformation',component: ProjectInformation}
      ]
    },
    {path: '/user/home',
      component: Home,
      children:[
        {path: '/userList',component: UserList},
        {path: '/giveUserToGroup',component: GiveUserToGroup},
        {path: '/myGroup',component: MyGroup},
        {path: '/createPlan',component: CreatePlan},
        {path: '/uploadFile',component: UploadFile},
        {path: '/getDailyFile',component: DailyFile},
        {path: '/getProjectFile',component: ProjectFile},
        {path: '/getBook',component: Book}

      ]
    },


  ]
});

//挂载路由导航守卫（路由拦截）
router.beforeEach((to,from,next) =>{
  //to将要访问的路径
  //from 代表那个路径跳转而来
  //next 是一个函数，表示放行  next('/login') 强制跳转
  //如果当前用户处于注册页,直接放行
  if (to.path === '/register'){
    return next();
  }
  //如果当前用户处于更新密码页,直接放行
  if (to.path === '/updatePassword'){
    return next();
  }
  //如果当前用户处于登录页,直接放行
  if (to.path === '/login'){
    return next();
  }
  //获取后端token
  const tokenStr = window.sessionStorage.getItem("token");
  //如果token失效，返回登录页
  if (!tokenStr){
    return next('/login');
  }
  return next();
});
export default router;
